package org.neptune.service.rabbit;

import org.neptune.api.entity.CacheBean;
import org.neptune.common.constant.CharacterConstant;
import org.neptune.common.constant.NeptuneConstant;
import org.neptune.service.config.NeptuneConfig;
import org.neptune.service.redis.RedisRepository;
import org.neptune.service.redis.RedissonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author lwl
 * @date 2020/10/21 11:41 下午
 */
@Component
@RabbitListener(queues = NeptuneConstant.SECKILL_DELAY_QUEUE_NAME)
public class SeckillDelayConsumer {

	private static final Logger logger = LoggerFactory.getLogger(SeckillDelayConsumer.class);

	@Autowired
	private RedisRepository redisRepository;

	@Autowired
	private NeptuneConfig neptuneConfig;

	@RabbitHandler
	public void processDelaySeckill(CacheBean cacheBean) {
		String cacheUserKey = RedissonUtil.getRedisKey(NeptuneConstant.REDIS_SECKILL_USER_ORDER,
				cacheBean.getUserId() + CharacterConstant.HYPHEN + cacheBean.getSeckillId());

		if (redisRepository.hasKey(cacheUserKey)){
			redisRepository.remove(cacheUserKey);
			String cacheCountKey = RedissonUtil.getRedisKey(NeptuneConstant.REDIS_SECKILL_GOODS_COUNT,
					cacheBean.getSeckillId());
			// 订单数量还原
			redisRepository.increment(cacheCountKey, neptuneConfig.seckillInfoCacheTime);
		}
	}

}
